/*
 * @(#) $Header$
 *
 * Copyright (C)  2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package ca.forklabs.baselib.util.function;

import ca.forklabs.baselib.util.BinaryFunction;
import ca.forklabs.baselib.util.UnaryFunction;

/**
 * A {@code Bind1stFunction} is a function adapter that transforms a
 * {@link BinaryFunction} into an {@link UnaryFunction} by binding the first
 * argument of the binary function.
 *
 * @param   <R>   the type of the returned object.
 * @param   <A1>   the type of the first argument object.
 * @param   <A2>   the type of the second argument object.
 *
 * @author <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.function.Bind1stFunction">Daniel Léonard</a>
 * @version $Revision$
 */
public class Bind1stFunction<R, A1, A2> implements UnaryFunction<R, A2> {

//---------------------------
// Instance variable
//---------------------------

   /** The adapted binary function. */
   private BinaryFunction<R, A1, A2> the_function;

   /** The binded value. */
   private A1 the_arg1;


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    * @param   function   the binary function to adapt.
    * @param   arg1   the binded argument.
    * @exception   NullPointerException   if the function parameter is
    *                                     {@code null}.
    */
   public Bind1stFunction(BinaryFunction<R, A1, A2> function, A1 arg1) {
      this.setFunction(function);
      this.setArg1(arg1);
      }


//---------------------------
// Accessor and mutator
//---------------------------

   /**
    * Changes the binary function.
    * @param   function   the new binary function.
    * @exception   NullPointerException   if the function is <code>null</code>.
    */
   @SuppressWarnings("nls")
   protected void setFunction(BinaryFunction<R, A1, A2> function) {
      if (null == function) {
         String message = this.getNullFunctionErrorMessage("f(c, x)");
         throw new NullPointerException(message);
         }
      this.the_function = function;
      }

   /**
    * Returns the adapted binary function.
    * @return   the adapted binary function.
    */
   protected BinaryFunction<R, A1, A2> getFunction() {
      return this.the_function;
      }

   /**
    * Changes the binded argument.
    * @param   arg1   the new binded argument.
    */
   protected void setArg1(A1 arg1) {
      this.the_arg1 = arg1;
      }

   /**
    * Returns the binded argument.
    * @return   the binded argument.
    */
   protected A1 getArg1() {
      return this.the_arg1;
      }


//---------------------------
// Implemented method from ca.forklabs.baselib.util.UnaryFunction
//---------------------------

   /**
    * Invokes the adapted function with the binded argument as the first
    * argument and the parameter as the second argument.
    * @param  arg2   the second argument.
    * @return   the results returned by the adapted function.
    */
   @Override
   public R invoke(A2 arg2) {
      BinaryFunction<R, A1, A2> function = this.getFunction();
      A1 arg1 = this.getArg1();

      R result = function.invoke(arg1, arg2);
      return result;
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the error message saying that a function is {@code null}.
    * @param   function   the null function.
    * @return   the error message.
    */
   protected String getNullFunctionErrorMessage(String function) {
      String key = Resources.COMPOSE_NULL_FUNCTION;
      String message = Resources.getLocalizedString(key, function);
      return message;
      }

   }
